import constant
from django.db import models
from django.contrib.auth.models import AbstractUser
from luffyapi.settings import constants
from luffyapi.utils.models import BaseModel


# Create your models here.
class User(AbstractUser):
    # unique=True 同一个值在同一个表同一个字段中只能出现一次，唯一索引　
    mobile = models.CharField(verbose_name="手机号码", max_length=15, unique=True)
    avatar = models.ImageField(upload_to='avatar', verbose_name='用户头像', null=True, blank=True,
                               help_text="头像图片的大小规格：256x256，或者对应的比例的图片")
    credit = models.IntegerField(default=0, null=True, blank=True, verbose_name="用户积分",
                                 help_text="用户积分换算比例： 1RMB=%s积分" % constants.RMB_CREDIT)

    class Meta:
        db_table = 'ly_users'
        verbose_name = '用户'
        verbose_name_plural = verbose_name


class Credit(BaseModel):
    """积分流水模型"""
    opera_choices = (
        (0, "业务充值"),
        (1, "购物消费"),
        (2, "系统赠送"),
    )
    operation = models.SmallIntegerField(choices=opera_choices, default=1, verbose_name="积分操作类型")
    number = models.IntegerField(default=0, verbose_name="积分数量")
    user = models.ForeignKey(User, related_name='user_credits', on_delete=models.CASCADE, verbose_name="用户")

    class Meta:
        db_table = 'ly_credit'
        verbose_name = '积分'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" % self.id
